/* ================ 深拷贝 ================ */
//返回传递给他的任意对象的类
const isClass = o => {
  if (o === null) return "Null";
  if (o === undefined) return "Undefined";
  return Object.prototype.toString.call(o).slice(8, -1);
};

//深度克隆
const deepClone = obj => {
  if (!obj) {
    return null;
  }
  let result,
    oClass = isClass(obj);
  //确定result的类型
  if (oClass === "Object") {
    result = {};
  } else if (oClass === "Array") {
    result = [];
  } else {
    return obj;
  }

  for (let key in obj) {
    if (obj.hasOwnProperty(key)) {
      let copy = obj[key];
      if (isClass(copy) == "Object") {
        result[key] = deepClone(copy); //递归调用
      } else if (isClass(copy) == "Array") {
        result[key] = deepClone(copy);
      } else {
        result[key] = obj[key];
      }
    }
  }
  return result;
};

// 身份证验证
function ValidateIdCard(idcard) {
  const reg = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
  return reg.test(idcard);
}
//  开户行账号格式校验

function checkOpeningBankAccount(account) {
  const reg = /^([1-9]{1})(\d{11}|\d{15}|\d{16}|\d{17}|\d{18})$/;
  return reg.test(account);
}
// 邮箱正则校验
function email(email) {
  const reg = /^[A-Za-z0-9-._]+@[A-Za-z0-9-]+(.[A-Za-z0-9]+)*(.[A-Za-z]{2,6})$/;
  return reg.test(email);
}
// 手机正则校验
function telPhone(phone) {
  const reg = /^1[3|4|5|7|8][0-9]{9}$/;
  return reg.test(phone);
}

// 格式化成年月日
// 使用方式
// var year = getDateTime('year');  
// console.log(year);  // 2021
// var month = getDateTime('month');  
// console.log(month); // 2021-12
// var date = getDateTime('');
// console.log(date);  // 2021-12-03 09:00:00
// 日期格式化年月日函数
function formatDate(type, format) {
	var date = new Date();
	var hengGang = "-";
	var maoHao = ":";
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	var curDate = date.getDate();
	var curHours = date.getHours();
	var curMinutes = date.getMinutes();
	var curSeconds = date.getSeconds();
	if (month >= 1 && month <= 9) {
		month = "0" + month;
	}
	if (curDate >= 0 && curDate <= 9) {
		curDate = "0" + curDate;
	}
	if (curHours >= 0 && curHours <= 9) {
		curHours = "0" + curHours;
	}
	if (curMinutes >= 0 && curMinutes <= 9) {
		curMinutes = "0" + curMinutes;
	}
	if (curSeconds >= 0 && curSeconds <= 9) {
		curSeconds = "0" + curSeconds;
	}
	var currentdate = "";
	if (type == 'format') {
		// 传递需要的日期 格式化成年月日 只要年月日  format是传递的日期参数
		currentdate = format.slice(0, 16)
		return currentdate;
	} else if (type == "year") {
		// 获取年份
		currentdate = year;
		return currentdate;
	} else if (type == "month") {
		// 获取年份月份
		currentdate = year + hengGang + month
		return currentdate
	} else if (type == "day") {
		// 获取年月日
		currentdate = year + hengGang + month + hengGang + curDate
		return currentdate
	} else if (type == "minutes") {
		// 获取年月日 时分
		currentdate =
			year + hengGang + month + hengGang + curDate + ' ' + curHours + maoHao + curMinutes
	} else {
		// 获取年月日 + 当前时间
		currentdate =
			year + hengGang + month + hengGang + curDate + ' ' + curHours + maoHao + curMinutes + maoHao + curSeconds
		return currentdate
	}
}

// 获取当前日期
function getNowFormatDate() {
  var date = new Date();
  var seperator1 = "-";
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var strDate = date.getDate();
  if (month >= 1 && month <= 9) {
      month = "0" + month;
  }
  if (strDate >= 0 && strDate <= 9) {
      strDate = "0" + strDate;
  }
  var currentdate = year + seperator1 + month + seperator1 + strDate;
  return currentdate;
}




function time_to_sec(time) {
  var s = "";

  var hour = time.split(":")[0];
  var min = time.split(":")[1];
  var sec = time.split(":")[2];

  s = Number(hour * 3600) + Number(min * 60) + Number(sec);

  return s;
}
// 获取当天的前七天日期函数
function getBeforeDate(n) {
  console.log("开始调用日期函数了", n);
  var n = n;
  var d = new Date();
  var year = d.getFullYear();
  var mon = d.getMonth() + 1;
  var day = d.getDate();
  if (day <= n) {
    if (mon > 1) {
      mon = mon - 1;
    } else {
      year = year - 1;
      mon = 12;
    }
  }
  d.setDate(d.getDate() - n);
  year = d.getFullYear();
  mon = d.getMonth() + 1;
  day = d.getDate();
  var s =
    year +
    "-" +
    (mon < 10 ? "0" + mon : mon) +
    "-" +
    (day < 10 ? "0" + day : day);
  return s;
}
// 时间戳转换成日期时间
function getYMDHMS(timestamp) {
  let time = new Date(timestamp);
  let year = time.getFullYear();
  let month = time.getMonth() + 1;
  let date = time.getDate();
  let hours = time.getHours();
  let minute = time.getMinutes();
  let second = time.getSeconds();
  if (month < 10) {
    month = "0" + month;
  }
  if (date < 10) {
    date = "0" + date;
  }
  if (hours < 10) {
    hours = "0" + hours;
  }
  if (minute < 10) {
    minute = "0" + minute;
  }
  if (second < 10) {
    second = "0" + second;
  }
  return (
    year + "-" + month + "-" + date + " " + hours + ":" + minute + ":" + second
  );
}

function timestampToTime(timestamp) {
  var date = new Date(timestamp * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
  var Y = date.getFullYear() + "-";
  var M =
    (date.getMonth() + 1 < 10
      ? "0" + (date.getMonth() + 1)
      : date.getMonth() + 1) + "-";
  var D = date.getDate() + " ";
  var h = date.getHours() + ":";
  var m = date.getMinutes() + ":";
  var s = date.getSeconds();
  return Y + M + D + h + m + s;
}

function year() {
  var myDate = new Date();
  var year = myDate.getYear(); //获取当前年份(2位)
  return year;
}

function month() {
  var myDate = new Date();
  var month = myDate.getMonth();
  return month;
}

function setData(obj, callback) {
  let that = this;
  const handleData = (tepData, tepKey, afterKey) => {
    tepKey = tepKey.split(".");
    tepKey.forEach(item => {
      if (tepData[item] === null || tepData[item] === undefined) {
        let reg = /^[0-9]+$/;
        tepData[item] = reg.test(afterKey) ? [] : {};
        tepData = tepData[item];
      } else {
        tepData = tepData[item];
      }
    });
    return tepData;
  };
  const isFn = function(value) {
    return typeof value == "function" || false;
  };
  Object.keys(obj).forEach(function(key) {
    let val = obj[key];
    key = key.replace(/\]/g, "").replace(/\[/g, ".");
    let front, after;
    let index_after = key.lastIndexOf(".");
    if (index_after != -1) {
      after = key.slice(index_after + 1);
      front = handleData(that, key.slice(0, index_after), after);
    } else {
      after = key;
      front = that;
    }
    if (front.$data && front.$data[after] === undefined) {
      Object.defineProperty(front, after, {
        get() {
          return front.$data[after];
        },
        set(newValue) {
          front.$data[after] = newValue;
          that.$forceUpdate();
        },
        enumerable: true,
        configurable: true
      });
      front[after] = val;
    } else {
      that.$set(front, after, val);
    }
  });
  // this.$forceUpdate();
  isFn(callback) && this.$nextTick(callback);
}
module.exports = {
  timestampToTime: timestampToTime,
  ValidateIdCard: ValidateIdCard,
  checkOpeningBankAccount: checkOpeningBankAccount,
  getYMDHMS,
  setData,
  email,
  telPhone,
  getBeforeDate,
  deepClone,
  formatDate,
  getNowFormatDate,
  isClass
};
